﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WebApp.BLL.Base;
using WebApp.Common;
using WebApp.Common.Caching;
using WebApp.DAL;
using WebApp.Models;

namespace WebApp.BLL.Basic
{
    public class S_documentService : BaseService<S_document>
    {

        #region 构造函数
        public S_documentService() : base(new S_documentRepository()) { }

        #endregion

        #region PageList

        public List<S_document> PageList(int pageindex,
            int pagesize,
            string searchkey,
            string sort,
            string order,
            out int rowscount)
        {
            var T = this.CurrentRepository.Entities.Where(p => p.Isdeleted == 0);
            T = T.Where(p => p.Isdeleted == 0);

            //查询条件
            if (!string.IsNullOrEmpty(searchkey))
            {
                T = T.Where(p => p.Title.Contains(searchkey) || p.TitleHK.Contains(searchkey));
            }

            if (string.IsNullOrEmpty(sort)) sort = "Id";
            T = base.SortList(T, sort, order == "asc" ? true : false);
            //返回记录数
            rowscount = T.Count();
            //返回分页结果
            return this.CurrentRepository.PageList(T, pageindex, pagesize).ToList();
        }

        #endregion

        #region DeleteById

        public bool DeleteById(int id)
        {
            var item = this.CurrentRepository.Find(id);

            if (item != null)
            {
                item.Isdeleted = 1;
                this.CurrentRepository.Update(item);
            }
            
            return true;
        }

        #endregion

        #region DeleteByIds

        public bool DeleteByIds(string ids)
        {
            foreach (string id in ids.Split(','))
            {
                if (string.IsNullOrEmpty(id)) continue;

                this.DeleteById(Utils.NvInt(id));
            }

            return true;
        }

        #endregion

        #region Exist
        public bool Exist(int id, string dockey)
        {
            if (id == 0)
            {
                return this.CurrentRepository.Entities.FirstOrDefault(p => p.DocKey == dockey && p.Isdeleted == 0) != null;
            }
            else
            {
                return this.CurrentRepository.Entities.FirstOrDefault(p => p.Id != id && p.DocKey == dockey && p.Isdeleted == 0) != null;
            }
        }
        #endregion

        #region Save

        public bool Save(S_document company)
        {
            if (company.Id == 0)
            {
                this.CurrentRepository.Add(company);
            }
            else
            {
                this.CurrentRepository.Update(company);
            }

            return true;
        }

        #endregion

        #region SelectByDocKey

        public S_document SelectByDocKey(string dockey)
        {
            var item = this.CurrentRepository.Entities.FirstOrDefault(p => p.Isdeleted == 0 && p.DocKey == dockey);

            return item;
        }

        #endregion

        #region GetContent

        public string GetContent(string dockey, string lang = "")
        {
            var item = this.CurrentRepository.Entities.FirstOrDefault(p => p.Isdeleted == 0 && p.DocKey == dockey);
            if (item == null)
            {
                return "";
            }

            if ("zh_hk".Equals(lang, StringComparison.OrdinalIgnoreCase))
            {
                return item.ContentHK;
            }
            else
            {
                return item.Content;
            }
        }

        #endregion

    }
}
